#include <bits/stdc++.h>
// 2025/02/23
// tag: 
// Author: Forever
using namespace std;

#define int long long

using int64 = int64_t;

int help(int a, int b, int c, int d){
    int cnt = 0;
    while (true){
        if (c == a && d == b)
            return cnt;
        if (c < a || d < b || (c > d && d < b) || (d > c && c < a) || (c == d))
            return -1;
        if (c > d){
            int k = (c - a) / d;
            if (k == 0)
                return -1;
            cnt += k;
            c -= k * d;
        }
        else{
            int k = (d - b) / c;
            if (k == 0)
                return -1;
            cnt += k;
            d -= k * c;
        }
    }
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    for (int i = 1; i <= t; i++){
        int a, b, c, d;
        cin >> a >> b >> c >> d;
        cout << help(a, b, c, d) << endl;
    }
    return 0;
}